// 导入 express 模块
const express = require("express");
const fs = require("fs");
const path = require("path");

let p = path.resolve(__dirname, "./404.html");
// let p = path.resolve(__dirname, "./404.html");
console.log(p);
const data = fs.readFileSync(p, "utf8");
console.log(data);

// 创建应用对象
const app = express();

// function routemiddleware(req, res, next) {
let checkRouteMiddleware = (req, res, next) => {
  if (req.query.code == "521") {
    next();
  } else {
    res.send("暗号错误");
  }
};

// 创建路由
app.get("/home", (req, res) => {
  res.send("前台首页");
});

app.get("/admin", checkRouteMiddleware, (req, res) => {
  res.send("后台首页");
});

app.get("/settings", checkRouteMiddleware, (req, res) => {
  res.send("设置首页");
});

app.all("*", (req, res) => {
  res.send(data);
});

// 监听端口，启动服务
app.listen(8000, () => {
  console.log("服务已经启动……路由中间件示例");
});
